package com.zzyy.study.controller;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.update.LambdaUpdateWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.zzyy.study.common.Result;
import com.zzyy.study.common.ResultUtils;
import com.zzyy.study.common.PageResult;
import com.zzyy.study.entities.User;
import com.zzyy.study.service.UserService;
import jakarta.annotation.Resource;
import lombok.extern.slf4j.Slf4j;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * @auther zzyy
 * @create 2024-06-21 16:54
 */
@RestController
@Slf4j
public class UserController
{
    @Resource
    private UserService userService;

    @PostMapping(value = "/user/add")
    public boolean add(@RequestBody User user)
    {
        return userService.save(user);
    }

    @DeleteMapping(value = "/user/del/{id}")
    public boolean del(@PathVariable("id") Integer id)
    {
        return userService.removeById(id);
    }

    @PutMapping(value = "/user/update")
    public boolean update(@RequestBody User user)
    {

        User oldUser = userService.getBaseMapper().selectById(user.getId());

        LambdaUpdateWrapper<User> updateWrapper = new LambdaUpdateWrapper<>();


        updateWrapper
                .set(User::getUsername,user.getUsername())
                .set(User::getPassword,user.getPassword())
                .set(User::getSex,user.getSex())
                .set(User::getCreateTime,user.getCreateTime())
                .set(User::getVersion,oldUser.getVersion()+1)
                .eq(User::getId,user.getId())
                .eq(User::getVersion,oldUser.getVersion());

        return userService.update(updateWrapper);
    }

    @PutMapping(value = "/user/updateConcurrent")
    public boolean updateConcurrent(@RequestBody User user)
    {

        User oldUser1 = userService.getBaseMapper().selectById(user.getId());
        User oldUser2 = userService.getBaseMapper().selectById(user.getId());

        oldUser1.setPassword("111111");
        userService.getBaseMapper().updateById(oldUser1);

        oldUser2.setPassword("222222");
        int count = userService.getBaseMapper().updateById(oldUser2);

        if(count == 0)
        {
            System.out.println("oldUser2=====update fail");
            return false;
        }else{
            System.out.println("oldUser2=====update success");
            return true;
        }
    }

    @GetMapping(value = "/user/get/{id}")
    public User getById(@PathVariable("id") Integer id)
    {
        return userService.getById(id);
    }

    @GetMapping(value = "/user/list")
    public List<User> list()
    {
        return userService.getBaseMapper().selectList(null);
    }

    @GetMapping(value = "/user/get")
    public User getByUseName(@RequestParam("username") String username)
    {
        LambdaQueryWrapper<User> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.eq(User::getUsername,username);

        return userService.getOne(queryWrapper);
    }


    /**2024.6.30
     * MybatisPlus原生分页插件IPage增强30
     *
     * @param pageNum
     * @param pageSize
     * @return
     */
    //GET http://localhost:24623/user/selectPage?pageNum=1&pageSize=2
    @GetMapping("/user/selectPage")
    public List<User> selectPage(@RequestParam("pageNum") int pageNum, @RequestParam("pageSize") int pageSize)
    {
        IPage<User> page = new Page(pageNum,pageSize);
        return userService.page(page).getRecords();
    }

    /**2024.6.30
     * PageHelper分页插件普通使用V1
     * @param pageNum
     * @param pageSize
     * @return
     */
    //  http://localhost:24623/user/selectpagehelperV1?pageNum=1&pageSize=2
    @GetMapping("/user/selectpagehelperV1")
    public Result<PageResult<User>> selectPageHelperV1(@RequestParam("pageNum") int pageNum,
                                                 @RequestParam("pageSize") int pageSize)
    {
        PageResult<User> page = userService.selectPageHelperV1(pageNum, pageSize);
        return ResultUtils.success(page);
    }

    /**2024.6.30
     * PageHelper分页插件+带条件普通使用V2
     * @param pageNum
     * @param pageSize
     * @param username
     * @param password
     * @return
     */
    //  http://localhost:24623/user/selectpagehelperV2?pageNum=1&pageSize=2&username=黄忠&password=666666
    @GetMapping("/user/selectpagehelperV2")
    public Result<PageResult<User>> selectPageHelperV2(@RequestParam("pageNum") int pageNum,
                                                 @RequestParam("pageSize") int pageSize,
                                                 @RequestParam("username") String username,
                                                 @RequestParam("password") String password) {
        PageResult<User> page = userService.selectPageHelperV2(pageNum, pageSize,username,password);
        return ResultUtils.success(page);
    }

    /**2024.6.30
     * PageHelper + 容器捞鱼V2(AOP+反射+参数捞鱼封装)
     * @param pageNum
     * @param pageSize
     * @return
     */
    //  http://localhost:24623/user/selectPageHelperAOP?pageNum=2&pageSize=3
    @GetMapping("/user/selectPageHelperAOP")
    public Result<PageResult<User>> selectPageHelperAOP(@RequestParam("pageNum") int pageNum,
                                                  @RequestParam("pageSize") int pageSize) {
        PageResult<User> page = userService.selectPageHelperAOP(pageNum, pageSize);
        return ResultUtils.success(page);
    }
}




/*
    PUT http://localhost:24611/user/update
        Content-Type: application/json

        {
        "id": 56,
        "username": "曹洪",
        "password": "123777",
        "sex": "1",
        "createTime": "2024-06-21 17:59:59"
        }*/


/*
###
        POST http://localhost:24611/user/add
        Content-Type: application/json

        {
        "username": "张龙",
        "password": "333444",
        "sex": "1"
        }*/
